<template>
	<block class="warp">
		<scroll-view class="box_content flex" scroll-y>
			<view class="select1">
				<uni-search-bar @confirm="query()" :focus="true" placeholder="输入关键词进行搜索" v-model="keyword1" />
				<Select style="width:100%" showTag="cate_name" :listArr="listArr" :itemArr="itemArr"
					@finish="finish"></Select>
			</view>
			<empty v-if="!data.length" style="width: 100%;"></empty>
			<view class="shop_item" v-if="data.length">
				<view class="item" v-for="(item,index) in data" :key="index" @click="gotoshopInfo(item.id)"
					@scrolltolower="scrolltolower">
					<image :src="item.image" style="width: 330rpx;height: 300rpx;border-radius: 10rpx;"
						mode="aspectFill"></image>
					<view class="name">{{item.store_name}}</view>
					<view class="price"><text class="word1">指导价:</text><text
							style="font-size: 24rpx;">￥</text>{{item.price}}</view>
					<view class="add iconfont cmain" @click.stop="add(item)">
						&#xe61c;
					</view>
				</view>
			</view>
		</scroll-view>
		<view class="shopcar">
			<view class="left" @click="look">
				已选{{shopcar.length}}件
			</view>
			<view class="right" @click="confirm">
				确认添加
			</view>
		</view>
		<uni-popup ref="popup" type="bottom">
			<view class="popup">
				<view class="shop_item1" v-if="data.length">
					<view class="item" v-for="(item,index) in shopcar" :key="index">
						<image :src="item.image" style="width: 80rpx;height: 80rpx;border-radius: 10rpx;"
							mode="aspectFill"></image>
						<view class="name">{{item.store_name}}</view>
						<view class="price"><text class="word1">指导价:</text><text
								style="font-size: 24rpx;">￥</text>{{item.price}}</view>
						<view class="add">
							<uni-number-box v-model="item.num" />
						</view>
					</view>
				</view>
			</view>
		</uni-popup>
	</block>
</template>

<script>
	import Select from '../../../components/dropdown-screen/dropdown-screen.vue'
	import empty from '../../../components/empty.vue'
	import uniSearchBar from '@/components/uni-ui/lib/uni-search-bar/uni-search-bar.vue'

	import {
		parts_products,
		parts_category
	} from '../../../api/api.js'
	export default {
		components: {
			Select,
			empty,
			uniSearchBar
		},
		data() {
			return {
				num: 0,
				shopcar: [],
				listArr: ['设备类型', '设备品牌'],
				itemArr: [],
				sid: "",
				cid: "",
				keyword1: "",
				data: [],
				page: 1,
				dearr: [],
				list: "",
			}
		},
		onLoad(opt) {
			this.list = opt.data
		},
		created() {
			this.gettype()
			this.dataInit()
			let data = uni.getStorageSync('pjshop')
			if (data.length == 0) return;
			this.shopcar = data
		},
		methods: {
			// 查看购物车
			look() {
				this.$refs.popup.open()

			},
			// 确定
			confirm() {
				console.log(this.shopcar)
				uni.setStorageSync('pjshop', this.shopcar)
				console.log(this.list)
				uni.navigateTo({
					url: `/pages/sfOrderInfo/sfOrderInfo?data=${this.list}`
				})
			},

			// 添加
			add(e) {
				console.log(e)
				if (this.shopcar.length == 0) {
					e.num = 1
					this.shopcar.push(e)
				}
				if (this.shopcar.length != 0) {
					let i = 0
					let flag = false
					this.shopcar.forEach(item => {
						i++
						if (item.id == e.id) {
							item.num++
							flag = true
						}
						if (i == this.shopcar.length && flag == false) {
							e.num = 1
							this.shopcar.push(e)
						}
					})
				}
				console.log(this.shopcar)
			},
			// 分页
			scrolltolower() {
				this.page++
				let data = {
					sid: this.sid,
					cid: this.cid,
					keyword: '',
					limit: 10,
					page: this.page,
				}
				parts_products(data).then(res => {
					this.data = this.data.concat(res.data)
				})
			},
			// 模糊查询
			query() {
				this.dataInit()
			},
			// 获取零配件分类
			gettype() {
				parts_category().then(res => {
					this.itemArr.push(res.data)
					this.itemArr[1] = res.data[0].children
				})
			},
			// itemClick
			finish(e) {
				console.log('选择', e)
				if (e.$index == 0) {
					this.cid = e.id
					if (e.children.length != 0) {
						this.itemArr[1] = e.children
					} else {
						this.itemArr[1] = this.dearr
					}
				} else {
					this.sid = e.id
				}
				this.dataInit()
			},
			// 初始化
			dataInit() {
				this.page = 1
				console.log('调用接口')
				let data = {
					sid: this.sid,
					cid: this.cid,
					keyword: this.keyword1,
					limit: 10,
					page: this.page,
				}
				parts_products(data).then(res => {
					this.data = res.data
				})
			},
			/* 跳转到详情页 */
			gotoshopInfo: function(shopID) {
				uni.navigateTo({
					url: '/pages/shopInfo/shopInfo?shopID=' + shopID
				})
			},

		}

	}
</script>

<style scoped lang="less">
	.home_top {
		/* padding-left: -25px; */
		background-image: url('http://xiaowen.cool:86/yjky/yjkybg.png');
		background-size: cover;
		width: 750upx;
		height: 370upx;
		padding: 20upx;
		box-sizing: border-box;
		/* background-color: #4cd964; */
		flex-direction: row;
		align-items: center;
	}

	.box_content {
		display: flex;
		position: absolute;
		top: 0;
		height: calc(100%);
		background-color: #FFFFFF;
		bottom: 0;
	}

	.select1 {
		position: fixed;
		width: 100%;
		top: 0;
		background-color: #FFFFFF;
		z-index: 99;
	}

	.shop_item {
		padding: 30rpx;
		width: 100%;
		display: flex;
		justify-content: flex-start;
		flex-wrap: wrap;
		margin-top: 160rpx;
		align-items: center;

		.item {
			margin-left: 10rpx;
			display: block;
			width: 45%;
			height: 400rpx;
			position: relative;
			margin-bottom: 30rpx;

			.name {
				position: absolute;
				top: 310rpx;
			}

			.add {
				position: absolute;
				top: 350rpx;

				right: 0;
			}

			.price {
				position: absolute;
				top: 355rpx;
				line-height: 40rpx;
				color: #FD3833;
				font-size: 35rpx;

				.word1 {
					color: #999999;
					font-size: 24rpx;
				}
			}
		}
	}

	.shop_item1 {
		padding: 30rpx;
		width: 100%;
		display: flex;
		justify-content: flex-start;
		flex-wrap: wrap;
		align-items: center;

		.item {
			margin-left: 10rpx;
			display: block;
			width: 100%;
			height: 120rpx;
			position: relative;
			margin-bottom: 30rpx;

			.name {
				position: absolute;
				top: 0rpx;
				left: 100rpx;
			}

			.add {
				position: absolute;
				top: 10rpx;
				right: 30rpx;
			}

			.price {
				position: absolute;
				top: 40rpx;
				line-height: 40rpx;
				color: #FD3833;
				left: 100rpx;
				font-size: 35rpx;

				.word1 {
					color: #999999;
					font-size: 24rpx;
				}
			}
		}
	}

	.shopcar {
		position: absolute;
		height: 120rpx;
		bottom: 0rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		width: 100%;

		.left {
			background-color: #007AFF;
			width: 50%;
			height: 120rpx;
			line-height: 120rpx;
			text-align: center;
			color: #FFFFFF;
		}

		.right {
			background-color: #09BB07;
			width: 50%;
			height: 120rpx;
			line-height: 120rpx;
			text-align: center;
			color: #FFFFFF;
		}
	}

	.popup {
		width: 100%;
		height: 500rpx;
		background-color: #FFFFFF;
	}
</style>
